package com.demo.listen;

import com.demo.Controller.UserController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@Component
public class MyHttpSessionListen implements HttpSessionAttributeListener {

    public Integer count=0;

    @Autowired
    public UserController userController;

//    @Override
//    public void sessionCreated(HttpSessionEvent se) {
//        count++;
//        se.getSession().getServletContext().setAttribute("count", count);
//    }
//
//    @Override
//    public void sessionDestroyed(HttpSessionEvent se) {
//        count--;
//        se.getSession().getServletContext().setAttribute("count", count);
//        userController.logout(se.getSession());
//    }

    public void attributeAdded(HttpSessionBindingEvent se) {
        count++;
        se.getSession().getServletContext().setAttribute("count", count);
    }

    public void attributeRemoved(HttpSessionBindingEvent se) {
        count--;
        se.getSession().getServletContext().setAttribute("count", count);
        userController.logout(se.getSession());
    }
}
